import 'dart:async';
import 'package:flutter/material.dart';
import '../demo/ui_demo.dart';

class MyDemo extends StatelessWidget implements Demo {
  MyDemo(this.title);
  @override
  String title;
  @override
  Widget build(BuildContext context) {
    return new HomePage(title: this.title,);
  }
}

class HomePage extends StatelessWidget {
  String title;
  HomePage({this.title});
  @override
  Widget build(BuildContext context) {
    return CounterProvider(
      counterModel: CounterModel(),
      child: Scaffold(
        appBar: AppBar(
          title: Text(this.title),
          actions: <Widget>[
            IconSource("Bloc","20"),
          ],
        ),
        body:BlocDemo(),
        floatingActionButton: FloatingActionButtonDemo(),
      ),
    );
  }
}
class BlocDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    CounterProvider counterProvider=CounterProvider.of(context);
    return StreamBuilder(
      stream: counterProvider.counterModel.streamControllerOut.stream,
      builder: (context,snapshot){
        return Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              ActionChip(
                label: Text("${snapshot.data==null?0:snapshot.data}"),
                onPressed: ()=>counterProvider.counterModel.sinkIn.add(1),
              ),
              OutlineButton(
                child: Text("get network data"),
                onPressed: ()=>counterProvider.counterModel.request(),
              ),
              ListTile(
                title: Text("日志："),
                subtitle: Text(counterProvider.counterModel.log),
              ),
            ],
          ),
        );
      },
    );
  }
}
class FloatingActionButtonDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    CounterProvider counterProvider=CounterProvider.of(context);
    return FloatingActionButton(
      child: Icon(Icons.add),
      onPressed: ()=>counterProvider.counterModel.sinkIn.add(1)
    );
  }
}

class CounterModel {
  String log="...";
  int _count=0;
  int get count=>_count;
  final StreamController streamControllerIn=StreamController();
  StreamSink get sinkIn=>streamControllerIn.sink;
  final StreamController streamControllerOut=StreamController();
  StreamSink get sinkOut=>streamControllerOut.sink;
  CounterModel(){
    streamControllerIn.stream.listen(onData);
  }
  void onData(value){
    _count+=1;
    streamControllerOut.sink.add(_count);
  }
  //模仿从后台获取数据
  void request() async{
     log=log+"后台获取数据...\n";
     int value= await getData();
    _count+=value;
    streamControllerOut.sink.add(_count);
  }
  Future<int> getData() async{
    await Future.delayed(Duration(seconds: 2));
    debugPrint("getData");
    log=log+"获取数据:2\n";
    return 2;
  }
}

class CounterProvider extends InheritedWidget {
  final CounterModel counterModel;
  const CounterProvider({
    Key key,
    this.counterModel,
    @required Widget child,
  })
      : assert(child != null),
        super(key: key, child: child);

  static CounterProvider of(BuildContext context) {
    return context.inheritFromWidgetOfExactType(
        CounterProvider) as CounterProvider;
  }

  @override
  bool updateShouldNotify(CounterProvider old) {
    return false;
  }
}


